class Solution {
public:
    int subarraysDivByK(vector<int>& nums, int k) {
        unordered_map<int,int> hash;
        int sum=0,x=0,ret=0;
        hash[0%k]=1;
        for(auto x:nums)
        {
            sum+=x;
            int r=(sum%k+k)%k;//修正后的余数
            if(hash.count(r)) ret+=hash[r];
            hash[r]++;
        }
        return ret;
    }
};
